package com.mtz.service.impl;
import java.util.Date;
import java.util.List;

import com.mtz.dateobject.QuestionHistory;
import com.mtz.form.QuesAnsForm;
import com.mtz.repository.QuestionHistoryRepository;
import com.mtz.service.QuestionHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 煜赜
 * @date 2018/12/4 4:17
 */
@Service
@Slf4j
public class QuestionHistoryServiceImpl implements QuestionHistoryService {

    @Autowired
    private QuestionHistoryRepository questionHistoryRepository;

    @Override
    public void addHistory(QuesAnsForm quesAnsForm) {
        QuestionHistory questionHistory = new QuestionHistory();
        questionHistory.setQuestionId(quesAnsForm.getId());
        questionHistory.setQuestion(quesAnsForm.getQuestion());
        questionHistory.setKeyword(quesAnsForm.getKeyword());
        questionHistory.setAnswer(quesAnsForm.getAnswer());
        questionHistory.setAuthor(quesAnsForm.getAuthor());
        questionHistory.setAngle(quesAnsForm.getAngle());

        List<QuestionHistory> questionHistoryList = questionHistoryRepository.findByQuestionId(quesAnsForm.getId());
        if (questionHistoryList.size() == 0) {
            questionHistory.setVersion(1);
        } else {
            Integer version = questionHistoryList.get(questionHistoryList.size()-1).getVersion();
            questionHistory.setVersion(version+1);
        }

        questionHistoryRepository.save(questionHistory);
    }

    @Override
    public List<QuestionHistory> findByQuestionId(Integer questionId) {

        return questionHistoryRepository.findByQuestionId(questionId);
    }
}
